﻿using System.Windows;
using System.Windows.Controls;

namespace Sidvall.UI.Wpf.Controls
{
    public partial class HeaderedXamNumericInput : UserControl
    {
        #region Public Members

        #region LabelMargin

        public static readonly System.Windows.DependencyProperty LabelMarginProperty = System.Windows.DependencyProperty.Register(nameof(LabelMargin),
            typeof(Thickness),
            typeof(HeaderedXamNumericInput),
            new System.Windows.FrameworkPropertyMetadata(new Thickness(0, 0, 0, 0), System.Windows.FrameworkPropertyMetadataOptions.None,
                null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public Thickness LabelMargin
        {
            get
            {
                return (Thickness)GetValue(LabelMarginProperty);
            }
            set
            {
                SetValue(LabelMarginProperty, value);
            }
        }

        #endregion
        #region LabelText

        public static readonly System.Windows.DependencyProperty LabelTextProperty = System.Windows.DependencyProperty.Register(nameof(LabelText),
            typeof(string),
            typeof(HeaderedXamNumericInput),
            new System.Windows.FrameworkPropertyMetadata(null, System.Windows.FrameworkPropertyMetadataOptions.None,
                OnLabelTextPropertyChanged, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public string LabelText
        {
            get
            {
                return GetValue(LabelTextProperty) as string;
            }
            set
            {
                SetValue(LabelTextProperty, value);
            }
        }

        #endregion
        #region LabelLeftContent

        public static readonly System.Windows.DependencyProperty LabelLeftContentProperty = System.Windows.DependencyProperty.Register(nameof(LabelLeftContent),
            typeof(object),
            typeof(HeaderedXamNumericInput),
            new System.Windows.FrameworkPropertyMetadata(null, System.Windows.FrameworkPropertyMetadataOptions.None,
                null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public object LabelLeftContent
        {
            get
            {
                return GetValue(LabelLeftContentProperty);
            }
            set
            {
                SetValue(LabelLeftContentProperty, value);
            }
        }

        #endregion
        #region LabelRightContent

        public static readonly System.Windows.DependencyProperty LabelRightContentProperty = System.Windows.DependencyProperty.Register(nameof(LabelRightContent),
            typeof(object),
            typeof(HeaderedXamNumericInput),
            new System.Windows.FrameworkPropertyMetadata(null, System.Windows.FrameworkPropertyMetadataOptions.None,
                null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public object LabelRightContent
        {
            get
            {
                return GetValue(LabelRightContentProperty);
            }
            set
            {
                SetValue(LabelRightContentProperty, value);
            }
        }

        #endregion
        #region NumericBackground

        public static readonly System.Windows.DependencyProperty NumericBackgroundProperty = System.Windows.DependencyProperty.Register(nameof(NumericBackground),
            typeof(System.Windows.Media.Brush),
            typeof(HeaderedXamNumericInput),
            new System.Windows.FrameworkPropertyMetadata(System.Windows.Media.Brushes.White, System.Windows.FrameworkPropertyMetadataOptions.None,
                null, null, false, System.Windows.Data.UpdateSourceTrigger.LostFocus));
        public System.Windows.Media.Brush NumericBackground
        {
            get
            {
                return GetValue(NumericBackgroundProperty) as System.Windows.Media.Brush;
            }
            set
            {
                SetValue(NumericBackgroundProperty, value);
            }
        }

        #endregion
        #region NumericBorderBrush

        public static readonly System.Windows.DependencyProperty NumericBorderBrushProperty = System.Windows.DependencyProperty.Register(nameof(NumericBorderBrush),
            typeof(System.Windows.Media.Brush),
            typeof(HeaderedXamNumericInput),
            new System.Windows.FrameworkPropertyMetadata(new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 171, 173, 179)),
                System.Windows.FrameworkPropertyMetadataOptions.None, null, null, false, System.Windows.Data.UpdateSourceTrigger.LostFocus));
        public System.Windows.Media.Brush NumericBorderBrush
        {
            get
            {
                return GetValue(NumericBorderBrushProperty) as System.Windows.Media.Brush;
            }
            set
            {
                SetValue(NumericBorderBrushProperty, value);
            }
        }

        #endregion
        #region NumericBorderThickness

        public static readonly System.Windows.DependencyProperty NumericBorderThicknessProperty = System.Windows.DependencyProperty.Register(nameof(NumericBorderThickness),
            typeof(Thickness),
            typeof(HeaderedXamNumericInput),
            new System.Windows.FrameworkPropertyMetadata(new Thickness(1), System.Windows.FrameworkPropertyMetadataOptions.None,
                null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public Thickness NumericBorderThickness
        {
            get
            {
                return (Thickness)GetValue(NumericBorderThicknessProperty);
            }
            set
            {
                SetValue(NumericBorderThicknessProperty, value);
            }
        }

        #endregion
        #region NumericSpinButtonDisplayMode

        public static readonly System.Windows.DependencyProperty NumericSpinButtonDisplayModeProperty = System.Windows.DependencyProperty.Register(nameof(NumericSpinButtonDisplayMode),
            typeof(Infragistics.Controls.Editors.SpinButtonDisplayMode),
            typeof(HeaderedXamNumericInput),
            new System.Windows.FrameworkPropertyMetadata(Infragistics.Controls.Editors.SpinButtonDisplayMode.Never, System.Windows.FrameworkPropertyMetadataOptions.None,
                null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public Infragistics.Controls.Editors.SpinButtonDisplayMode NumericSpinButtonDisplayMode
        {
            get
            {
                return (Infragistics.Controls.Editors.SpinButtonDisplayMode)GetValue(NumericSpinButtonDisplayModeProperty);
            }
            set
            {
                SetValue(NumericSpinButtonDisplayModeProperty, value);
            }
        }

        #endregion
        #region NumericFontWeight

        public static readonly System.Windows.DependencyProperty NumericFontWeightProperty = System.Windows.DependencyProperty.Register(nameof(NumericFontWeight),
            typeof(System.Windows.FontWeight),
            typeof(HeaderedXamNumericInput),
            new System.Windows.FrameworkPropertyMetadata(System.Windows.FontWeights.Normal, System.Windows.FrameworkPropertyMetadataOptions.None,
                null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public System.Windows.FontWeight NumericFontWeight
        {
            get
            {
                return (System.Windows.FontWeight)GetValue(NumericFontWeightProperty);
            }
            set
            {
                SetValue(NumericFontWeightProperty, value);
            }
        }

        #endregion
        #region NumericHeight

        public static readonly System.Windows.DependencyProperty NumericHeightProperty = System.Windows.DependencyProperty.Register(nameof(NumericHeight),
            typeof(double),
            typeof(HeaderedXamNumericInput),
            new System.Windows.FrameworkPropertyMetadata(System.Double.NaN, System.Windows.FrameworkPropertyMetadataOptions.None, null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public double NumericHeight
        {
            get
            {
                return (double)GetValue(NumericHeightProperty);
            }
            set
            {
                SetValue(NumericHeightProperty, value);
            }
        }

        #endregion
        #region NumericHorizontalAlignment

        public static readonly System.Windows.DependencyProperty NumericHorizontalAlignmentProperty = System.Windows.DependencyProperty.Register(nameof(NumericHorizontalAlignment),
            typeof(HorizontalAlignment),
            typeof(HeaderedXamNumericInput),
            new System.Windows.FrameworkPropertyMetadata(HorizontalAlignment.Stretch, System.Windows.FrameworkPropertyMetadataOptions.None, null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public HorizontalAlignment NumericHorizontalAlignment
        {
            get
            {
                return (HorizontalAlignment)GetValue(NumericHorizontalAlignmentProperty);
            }
            set
            {
                SetValue(NumericHorizontalAlignmentProperty, value);
            }
        }

        #endregion
        #region NumericInvalidValueBehavior

        public static readonly System.Windows.DependencyProperty NumericInvalidValueBehaviorProperty = System.Windows.DependencyProperty.Register(nameof(NumericInvalidValueBehavior),
            typeof(Infragistics.Controls.Editors.InvalidValueBehavior),
            typeof(HeaderedXamNumericInput),
            new System.Windows.FrameworkPropertyMetadata(Infragistics.Controls.Editors.InvalidValueBehavior.Default, System.Windows.FrameworkPropertyMetadataOptions.None,
                null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public Infragistics.Controls.Editors.InvalidValueBehavior NumericInvalidValueBehavior
        {
            get
            {
                return (Infragistics.Controls.Editors.InvalidValueBehavior)GetValue(NumericInvalidValueBehaviorProperty);
            }
            set
            {
                SetValue(NumericInvalidValueBehaviorProperty, value);
            }
        }

        #endregion
        #region NumericIsEnabled

        public static readonly System.Windows.DependencyProperty NumericIsEnabledProperty = System.Windows.DependencyProperty.Register(nameof(NumericIsEnabled),
            typeof(bool),
            typeof(HeaderedXamNumericInput),
            new System.Windows.FrameworkPropertyMetadata(true, System.Windows.FrameworkPropertyMetadataOptions.None,
                null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public bool NumericIsEnabled
        {
            get
            {
                return (bool)GetValue(NumericIsEnabledProperty);
            }
            set
            {
                SetValue(NumericIsEnabledProperty, value);
            }
        }

        #endregion
        #region NumericIsReadOnly

        public static readonly System.Windows.DependencyProperty NumericIsReadOnlyProperty = System.Windows.DependencyProperty.Register(nameof(NumericIsReadOnly),
            typeof(bool),
            typeof(HeaderedXamNumericInput),
            new System.Windows.FrameworkPropertyMetadata(false, System.Windows.FrameworkPropertyMetadataOptions.None,
                null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public bool NumericIsReadOnly
        {
            get
            {
                return (bool)GetValue(NumericIsReadOnlyProperty);
            }
            set
            {
                SetValue(NumericIsReadOnlyProperty, value);
            }
        }

        #endregion
        #region NumericMask

        public static readonly System.Windows.DependencyProperty NumericMaskProperty = System.Windows.DependencyProperty.Register(nameof(NumericMask),
            typeof(string),
            typeof(HeaderedXamNumericInput),
            new System.Windows.FrameworkPropertyMetadata(null, System.Windows.FrameworkPropertyMetadataOptions.None,
                null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public string NumericMask
        {
            get
            {
                return GetValue(NumericMaskProperty) as string;
            }
            set
            {
                SetValue(NumericMaskProperty, value);
            }
        }

        #endregion
        #region NumericValueTypeName

        public static readonly System.Windows.DependencyProperty NumericValueTypeNameProperty = System.Windows.DependencyProperty.Register(nameof(NumericValueTypeName),
            typeof(string),
            typeof(HeaderedXamNumericInput),
            new System.Windows.FrameworkPropertyMetadata(null, System.Windows.FrameworkPropertyMetadataOptions.None,
                null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public string NumericValueTypeName
        {
            get
            {
                return GetValue(NumericValueTypeNameProperty) as string;
            }
            set
            {
                SetValue(NumericValueTypeNameProperty, value);
            }
        }

        #endregion
        #region NumericWidth

        public static readonly System.Windows.DependencyProperty NumericWidthProperty = System.Windows.DependencyProperty.Register(nameof(NumericWidth),
            typeof(double),
            typeof(HeaderedXamNumericInput),
            new System.Windows.FrameworkPropertyMetadata(System.Double.NaN, System.Windows.FrameworkPropertyMetadataOptions.None, null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public double NumericWidth
        {
            get
            {
                return (double)GetValue(NumericWidthProperty);
            }
            set
            {
                SetValue(NumericWidthProperty, value);
            }
        }

        #endregion
        #region Value

        public static readonly System.Windows.DependencyProperty ValueProperty = System.Windows.DependencyProperty.Register(nameof(Value),
            typeof(object),
            typeof(HeaderedXamNumericInput),
            new System.Windows.FrameworkPropertyMetadata(null, System.Windows.FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public object Value
        {
            get
            {
                return GetValue(ValueProperty);
            }
            set
            {
                SetValue(ValueProperty, value);
            }
        }

        #endregion

        #endregion
        #region Private Members

        #region InitializeLabel

        private static void InitializeLabel(HeaderedXamNumericInput parent)
        {
            if (string.IsNullOrWhiteSpace(parent.LabelText))
            {
                parent.Label.Visibility = Visibility.Collapsed;
            }
            else
            {
                parent.Label.Visibility = Visibility.Visible;
            }
        }

        #endregion

        #region OnLabelTextPropertyChanged

        private static void OnLabelTextPropertyChanged(System.Windows.DependencyObject d, System.Windows.DependencyPropertyChangedEventArgs e)
        {
            var parent = d as HeaderedXamNumericInput;
            if (parent == null)
                return;
            InitializeLabel(parent);
        }

        #endregion

        #endregion
        #region Constructors

        public HeaderedXamNumericInput()
        {
            InitializeComponent();
        }

        #endregion
    }
}
